﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FI.Core.IoC;
using FI.Core.Properties;
using FI.Core.SettingSource.Configuration;
using FI.Core.Utilities;

namespace FI.Core.SettingSource
{
    /// <summary>
    /// This is factory to create or get setting source.
    /// </summary>
    public static class SettingSourceFactory
    {
        #region Fields
        private static ISettingSource settingSource;
        private static Dictionary<string, ISettingSource> settingSources = new Dictionary<string, ISettingSource>();
        private static object syncHelper = new object();
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the setting source.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>The setting source.</returns>
        public static ISettingSource GetSettingSource(string name = null)
        {
            ISettingSource source;
            SettingSourceSettings settings;
            if (string.IsNullOrWhiteSpace(name))
            {
                if (null != settingSource)
                {
                    return settingSource;
                }

                settings = ConfigurationManager.GetSection(Constants.ConfigurationSections.SettingSource) as SettingSourceSettings;
                if (null != settings)
                {
                    return settingSource = settings.GetSettingSource();
                }
                DefaultAssemblyResolver assemblyResolver = new DefaultAssemblyResolver(Assemblies.GetAssemblies());
                source = new ReflectedServiceLocatorConfigurator(assemblyResolver).CreateInstance<ISettingSource>();
                if (null == source)
                {
                    throw new TypeLoadException(Resources.ExceptionCannotResolveTypeName.Format((object)"SettingSource"));
                }
                return settingSource = source;
            }

            if (settingSources.TryGetValue(name, out source))
            {
                return source;
            }
            lock (syncHelper)
            {
                if (settingSources.TryGetValue(name, out source))
                {
                    return source;
                }

                settings = ConfigurationManager.GetSection(Constants.ConfigurationSections.SettingSource) as SettingSourceSettings;
                if (null == settings)
                {
                    throw new ConfigurationErrorsException(Resources.ExceptionSettingSourceNotExists.Format((object)Constants.ConfigurationSections.SettingSource));
                }

                source = settings.GetSettingSource(name);
                settingSources[name] = source;
                return source;
            }
        }

        /// <summary>
        /// Changes the setting source.
        /// </summary>
        /// <param name="settingSource">The setting source.</param>
        public static void ChangeSettingSource(ISettingSource settingSource)
        {
            Guard.ArgumentNotNull(settingSource, "settingSource");
            if (SettingSourceFactory.settingSource != settingSource)
            {
                settingSource.SetAsCurrentSettingsSource();
                SettingSourceFactory.settingSource = settingSource;
                ServiceLocatorFactory.Reset();
            }
        }

        /// <summary>
        /// Changes the setting source.
        /// </summary>
        /// <param name="name">The name.</param>
        public static void ChangeSettingSource(string name)
        {
            Guard.ArgumentNotNullOrEmpty(name, "name");
            SettingSourceSettings settings = ConfigurationManager.GetSection(Constants.ConfigurationSections.SettingSource) as SettingSourceSettings;
            if (null == settings)
            {
                throw new ConfigurationErrorsException(Resources.ExceptionSettingSourceNotExists.Format((object)Constants.ConfigurationSections.SettingSource));
            }
            ChangeSettingSource(settings.GetSettingSource(name));
        }

        /// <summary>
        /// Resets this default setting source to null.
        /// </summary>
        public static void Reset()
        {
            if (null != settingSource)
            {
                ServiceLocatorFactory.Reset();
            }
            settingSource = null;
        }
        #endregion
    }
}
